package com.ruoyi.patrol.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PatrolPoint;
import com.ruoyi.patrol.service.IPatrolPointService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 巡查点位Controller
 * 
 * @author yepanpan
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/patrol/point")
public class PatrolPointController extends BaseController
{
    @Autowired
    private IPatrolPointService patrolPointService;

    /**
     * 查询巡查点位列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:list')")
    @GetMapping("/list")
    public TableDataInfo list(PatrolPoint patrolPoint)
    {
        startPage();
        List<PatrolPoint> list = patrolPointService.selectPatrolPointList(patrolPoint);
        return getDataTable(list);
    }

    /**
     * 导出巡查点位列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:export')")
    @Log(title = "巡查点位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatrolPoint patrolPoint)
    {
        List<PatrolPoint> list = patrolPointService.selectPatrolPointList(patrolPoint);
        ExcelUtil<PatrolPoint> util = new ExcelUtil<PatrolPoint>(PatrolPoint.class);
        util.exportExcel(response, list, "巡查点位数据");
    }


    /**
     * 获取巡查点位详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(patrolPointService.selectPatrolPointById(id));
    }

    /**
     * 新增巡查点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:add')")
    @Log(title = "巡查点位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PatrolPoint patrolPoint)
    {
                patrolPoint.setCreateBy(getUsername());
        return toAjax(patrolPointService.insertPatrolPoint(patrolPoint));
    }

    /**
     * 修改巡查点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:edit')")
    @Log(title = "巡查点位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PatrolPoint patrolPoint)
    {
                patrolPoint.setUpdateBy(getUsername());
        return toAjax(patrolPointService.updatePatrolPoint(patrolPoint));
    }

    /**
     * 删除巡查点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:remove')")
    @Log(title = "巡查点位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patrolPointService.deletePatrolPointByIds(ids));
    }


    @Log(title = "巡查路线点位管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('patrol:point:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PatrolPoint> util = new ExcelUtil<PatrolPoint>(PatrolPoint.class);
        List<PatrolPoint> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = patrolPointService.importData(dataList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PatrolPoint> util = new ExcelUtil<PatrolPoint>(PatrolPoint.class);
        util.importTemplateExcel(response, "巡查路线点位");
    }
}
